<?php

namespace app\admin\controller\store;

use app\common\controller\Backend;
use app\common\model\District;
use app\common\model\Order;
use app\common\model\User;
use think\Model;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Store extends Backend
{
    
    /**
     * Store模型对象
     * @var \app\common\model\Store
     */
    protected $model = null;
    protected $searchFields = 'id,name';
    protected $multiFields = 'if_ticket';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Store;

    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(['name', 'id'], false);

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach($list as &$item){
                $item['district_id'] = District::where('id', $item['district_id'])->value('name');
                $item['ticket'] = Order::where('store_id', $item['id'])->where('is_cancel', Order::IS_CANCEL_NOT)->where('is_paid', Order::IS_PAID_PAY)->count();
                $item['money'] = Order::where('store_id', $item['id'])->where('is_cancel', Order::IS_CANCEL_NOT)->where('is_paid', Order::IS_PAID_PAY)->sum('money') ?: 0;
                $item['user_count'] = User::where('store_id', $item['id'])->count();
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();

    }

    public function import()
    {
        parent::import();
    }

    public function orderlist($ids = null){
        $this->model = new Order();
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(['user.nickname', 'store.name'], true);

            $list = $this->model
                ->with('user,store')
                ->where($where)
                ->where('order.store_id', $ids)
                ->where('order.is_cancel', Order::IS_CANCEL_NOT)
                ->where(function($query){
                    $query->whereOr('is_paid', Order::IS_PAID_PAY);
                })
//                ->field('u.avatar,u.nickname,order.money,order.pay_time,order.id as order_id,s.name')
                ->order($sort, $order)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();

    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

}
